<?php
header("content-type:text/html; charset=utf-8");

if (!empty($_POST)) {

    //修改数据
    require_once('../db/db.php');

    $id = $_POST['id'];//类型
    $type = $_POST['type'];//类型
    $title = addslashes(htmlspecialchars($_POST['title']));//标题
    $content = addslashes(htmlspecialchars($_POST['content']));//内容
    $time = $_POST['time'];//时间
    $time = $time == "" ? "0000-00-00" : $_POST['time'];

    //更新语句
    $sql = "update news set  title='$title',content='$content',utime='$time' where id=$id";

    try {
        $connect = Db::getInstance()->connect();
    } catch (Exception $e) {
        echo '数据库链接失败:' . $e->getMessage();
        exit;
    }

    $result = mysqli_query($connect, $sql);

    if ($result) {
        echo "<script>alert('修改成功');window.location='index.php'</script>";
    } else {
        echo "<script>alert('修改失败');window.history.back();</script>";
    }

} else {

    //获取显示数据
    require_once('../db/db.php');
    $id = $_REQUEST['id'];//获取id

    if (empty($id)) {
        echo '参数错误';
        exit;
    }

    //获取数据库数据
    $sql = "select * from news where id = $id";
    $news = array();//保存数据
    try {
        $connect = Db::getInstance()->connect();
    } catch (Exception $e) {
        // $e->getMessage();
        echo '数据库链接失败:' . $e->getMessage();
        exit;
    }
    $result = mysqli_query($connect, $sql);

    if ($result) {
        $news = mysqli_fetch_array($result);//解析数据
    }
}


?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>增加新闻</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/add.css" rel="stylesheet">
</head>

<body>
<div class="col-sm-9  col-md-10">
    <form class="form-horizontal" method="post" action="edit.php">

        <input  type="hidden" name="id" value="<?php echo $news['id'] ?>">

        <div class="form-group">
            <label for="type" class="col-sm-2 control-label">类别</label>
            <div class="col-sm-10">

                <input class="form-control" id="type" type="text" name="type" value="<?php echo $news['type_new'] ?>"
                       disabled>

            </div>
        </div>

        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">标题</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="title" name="title" value="<?php echo $news['title'] ?>"
                       placeholder="请输入新闻标题"
                       required="required">
            </div>
        </div>

        <div class="form-group">
            <label for="content" class="col-sm-2 control-label">内容</label>
            <div class="col-sm-10">
                <textarea class="form-control" rows="3" id="content" name="content" placeholder="请输入新闻内容"
                          required="required"><?php echo $news['content'] ?></textarea>
            </div>
        </div>

        <div class="form-group">
            <label for="time" class="col-sm-2 control-label">时间</label>
            <div class="col-sm-10">
                <input class="form-control" type="date" id="time" name="time" placeholder="请输入新闻时间"
                       value="<?php echo $news['ctime'] ?>" required="required"/>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-success">修改</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>